<template>
    <div class="default-main ba-table-box">
        <TableHeader v-if="state.materialType === 'text'" :buttons="['refresh', 'add']"></TableHeader>
        <el-alert v-else class="ba-table-alert" show-icon>
            <template #default>
                <div class="ba-markdown">
                    <div>
                        1、图文、图片、音频、视频，需要您在微信公众号管理平台进行上传。<a href="https://mp.weixin.qq.com" target="_blank"
                            >微信公众号管理平台</a
                        >
                    </div>
                    <div>2、下方图文列表中，展示的是在微信公众平台已发布的图文信息，非已群发的信息。</div>
                </div>
            </template>
        </el-alert>
        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <el-tabs type="border-card" v-model="state.materialType" @tab-change="materialTypeChange">
            <div v-for="(item, index) in state.materialTypeList" :key="index">
                <el-tab-pane :label="item.label" :name="item.name">
                    <Table ref="tableRef"></Table>
                </el-tab-pane>
            </div>
        </el-tabs>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

const state = reactive({
    materialType: 'text',
    materialTypeList: [
        { label: '文本', name: 'text' },
        { label: '图文', name: 'news' },
        { label: '图片', name: 'image' },
        { label: '音频', name: 'voice' },
        { label: '视频', name: 'video' },
    ],
})

defineOptions({
    name: 'xmwechat/offiaccount/material',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/xmwechat.offiaccount.Material/'),
    {
        pk: 'id',
        column: [
            {
                label: t('xmwechat.offiaccount.material.title'),
                prop: 'title',
                align: 'center',
                operator: false,
                show: false,
            },
            {
                label: t('xmwechat.offiaccount.material.name'),
                prop: 'name',
                align: 'center',
                operator: false,
                show: false,
            },
            {
                label: t('xmwechat.offiaccount.material.image'),
                prop: 'image',
                align: 'center',
                render: 'image',
                operator: false,
                show: false,
            },
            {
                label: t('xmwechat.offiaccount.material.content'),
                prop: 'content',
                align: 'center',
                operator: false,
            },
            {
                label: t('xmwechat.offiaccount.material.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
                show: false,
            },
            {
                label: t('xmwechat.offiaccount.material.update_time'),
                prop: 'update_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                width: 200,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('Operate'),
                align: 'center',
                width: 100,
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: { type: 'text', status: '1', content: null },
    }
)

provide('baTable', baTable)

const materialTypeChange = (name: string) => {
    state.materialType = name
    baTable.table.filter!.search = [{ field: 'type', val: name, operator: '=' }]
    baTable.getIndex()

    //0标题 1名称 2图片 3内容 4创建时间 5更新时间 6操作
    if (state.materialType == 'text') {
        baTable.table.column[0].show = false
        baTable.table.column[1].show = false
        baTable.table.column[2].show = false
        baTable.table.column[3].show = true
        baTable.table.column[4].show = true
        baTable.table.column[5].show = true
        baTable.table.column[6].show = true
    } else if (state.materialType == 'news') {
        baTable.table.column[0].show = true
        baTable.table.column[1].show = false
        baTable.table.column[2].show = false
        baTable.table.column[3].show = false
        baTable.table.column[5].show = true
        baTable.table.column[6].show = false
    } else if (state.materialType == 'image' || state.materialType == 'voice' || state.materialType == 'video') {
        baTable.table.column[0].show = false
        baTable.table.column[1].show = true
        baTable.table.column[2].show = false
        baTable.table.column[3].show = false
        baTable.table.column[5].show = true
        baTable.table.column[6].show = false
    }
}
onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.table.filter!.search = [{ field: 'type', val: state.materialType, operator: '=' }]
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
.ba-table-alert {
    margin: 15px 0;
}
</style>
